

//
//  HomeCollectionTopView.m
//  WanCaiC8
//
//  Created by tangang on 2018/6/8.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "Home_PlanCollectionTopView.h"
#import "AdvertisingView.h"

@interface Home_PlanCollectionTopView()
@property(nonatomic,strong)AdvertisingView *oneAdvertisingView;
@property(nonatomic,strong)UIView *line;


@end
@implementation Home_PlanCollectionTopView{
    UIView      *whiteView;
    QXLable     *labTitle;
    QXLable     *labAdvertising;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor whiteColor];
        //        whiteView =[[UIView alloc]init];
        //        whiteView.backgroundColor=[UIColor whiteColor];
        //        [self addSubview:whiteView];
        //        [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.right.top.equalTo(self);
        //            make.height.mas_equalTo(@(real_size(10)));
        //        }];
        
        
        
        
        [self addSubview:self.oneAdvertisingView];
        [self.oneAdvertisingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.mas_equalTo(@0);
        }];
        
        
      
        
        UIView *bgView=[[UIView alloc]init];
        bgView.backgroundColor=DEFAULTBGCOLOR;
        [self addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self.oneAdvertisingView.mas_bottom);
            make.height.mas_equalTo(@(real_size(40)));
        }];

        UIImageView *imageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fire"]];
        [bgView addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView).with.offset(17);
            make.centerY.equalTo(bgView);
            make.size.mas_equalTo(imageV.image.size);
        }];
        
        labTitle=[[QXLable alloc]initWithBFount:15];
        [bgView addSubview:labTitle];
        [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView);
            make.left.equalTo(imageV.mas_right).with.offset(6);
        }];
        
        labAdvertising=[[QXLable alloc]initGaryText];
        labAdvertising.userInteractionEnabled=YES;
        labAdvertising.textAlignment=NSTextAlignmentRight;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickAdvertising)];
        [labAdvertising addGestureRecognizer:tap];
        [bgView addSubview:labAdvertising];
        [labAdvertising mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTitle.mas_right).with.offset(5);
            make.centerY.equalTo(labTitle);
            make.right.equalTo(self).with.offset(-10);
        }];
        
        UIView *line2=[[UIView alloc]init];
        line2.backgroundColor=DEFAULTBGCOLOR;
        [bgView addSubview:line2];
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView).with.offset(17);
            make.right.equalTo(bgView).with.offset(-17);
            make.bottom.equalTo(bgView);
            make.height.mas_equalTo(@.7);
        }];
  
        
    }
    return self;
}

- (void)setTitle:(NSString *)title{
    _title=title;
    labTitle.text=title;
    if ([title isEqualToString:@"全国彩"]) {
        whiteView.hidden=YES;
    }else{
        whiteView.hidden=NO;
    }
}

- (void)setAdvertising:(AdvertisingModel *)advertising{
    _advertising=advertising;
    
    
    if ([advertising.Title containsString:@"【"]) {
        labAdvertising.attributedText=[NSAttributedString tg_imageTextWithTitle:advertising.Title] ;
    } else {
        labAdvertising.text=advertising.Title;
        
    }
}

- (void)onclickAdvertising{
    if (self.advertising.TransferUrl.length<1) {
        return;
    }
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString: self.advertising.TransferUrl]];
    
    
    
    if ([self.advertising.Title containsString:@"】"]) {
        
        UIPasteboard *copy = [UIPasteboard generalPasteboard];
        NSRange startRange = [self.advertising.Title rangeOfString:@"【"];
        NSRange endRange = [self.advertising.Title rangeOfString:@"】"];
        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
        NSString *result = [self.advertising.Title substringWithRange:range];
        
        [copy setString:result];
        
        [SVProgressHUD showSuccessWithStatus:@"复制成功"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
        });
        
    }else{
        if (self.advertising.TransferUrl.length<1) {
            return;
        }
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString: self.advertising.TransferUrl]];
        
    }
    
}


-(void)setAdModelArr:(NSMutableArray *)adModelArr{
    _adModelArr = adModelArr;
    
    self.oneAdvertisingView.dataArr = adModelArr;
    
    self.oneAdvertisingView.changeHeight(adModelArr.count*40);
}


- (AdvertisingView *)oneAdvertisingView{
    if (!_oneAdvertisingView) {
        WS(weakSelf);
        _oneAdvertisingView =[[AdvertisingView alloc]init];
        self.oneAdvertisingView.changeHeight = ^(float height) {
            [weakSelf.oneAdvertisingView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(height));
            }];
        };
    }
    return _oneAdvertisingView;
}
//
//




@end

